package content.views;
import content.views.works.DemoPlayer;
import content.views.works.LayerWorks;
import content.views.works.NavItemWorks;
import content.views.works.NavWorks;
import content.views.works.ScreenWorks;
import js.Lib;
import params.GlobalParams;
import tools.ASprite;
import tools.BoundProvider;

/**
 * ...
 * @author PRASS
 */

class Works extends BaseView
{
	
	private var _nav 		: NavWorks;
	private var _screen 	: ScreenWorks;
	
	private var _data 		: Object;
	private var _works		: Array<Object>;
	
	private var _currentID	: Int;
	
	public function new(id : String) 
	{
		super(id);
		
		dom.css("background-image", "url(" + GlobalParams.BG_WORKS + ")");
		
		_works = new Array<Object>();
	}
	
	override public function build():Void 
	{
		html = '<div id="colProdz"></div><div id="prodz"></div>';
		
		container.append(html);
		
		_nav 	= new NavWorks( new JQuery("#colProdz") );
		_screen = new ScreenWorks( new JQuery("#prodz") );
		
		_nav.setWidth(0);
		
		untyped $.getJSON( 'data/works.json', _onLinksLoaded);		
	}
	
	private function _onLinksLoaded(obj:Object) : Void
	{
		_data = obj;
				
		var i : Int = 0;
	
		while ( i < 200 )
		{
			if ( _data.works.work[i] != null)
				_works.push(_data.works.work[i]);
			else
				break;
			
			i++;
		}
		
		_configure();
	}
	
	private function _configure() : Void
	{
		_nav.build( _works );
		_nav.dom.click( function (e) { _onClick(e); } );
	}
	
	private function _onClick(e) : Void
	{
		//safety
		if ( e.target.id == _currentID) return;
		
		_currentID = cast e.target.id;
		
		_nav.handleSelect(_currentID);
		_screen.switchContent( _works[_currentID] );
	}
	
	override public function play():Void 
	{
		super.play();
		
		// anim in
		_nav.display();
		_screen.display();
	}
	
	override public function hide():Void 
	{
		// anim out
		_nav.hide();
		_screen.hide();
	}
	
	override public function resize(_w:Int, _h:Int):Void 
	{
		super.resize(_w, _h);
		_nav.resize(_w, _h);
		_screen.resize( _w - 216 -  100 , _h);
	}


	
}